package com.zou.mall.order.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.Map;
import java.util.UUID;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.zou.mall.order.vo.SubmitOrderVO;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.zou.mall.order.entity.OrderEntity;
import com.zou.mall.order.service.OrderService;
import com.zou.common.utils.PageUtils;
import com.zou.common.utils.R;


/**
 * 订单
 *
 * @author lingjin.zou
 * @email leonard_zou@163.com
 * @date 2022-02-14 14:17:25
 */
@RestController
@RequestMapping("order/order")
public class OrderController {

    @Autowired
    private OrderService orderService;

    @Autowired
    private RabbitTemplate rabbitTemplate;



    @GetMapping("/confirm-order")
    public R confirmOrder(){
        SubmitOrderVO submitOrderVO = orderService.confirmOrder();
        R r = new R();
        r.setData(submitOrderVO);
        return r;
    }

    @PostMapping("/submit-order")
    public String submitOrder(@RequestBody SubmitOrderVO submitOrderVO) {
        //订单下单成功
        orderService.submitOrder(submitOrderVO);
        return "ok";
    }

    @PostMapping("/submit-order-test")
    public String submitOrderTest() {
        //订单下单成功
        OrderEntity orderEntity = new OrderEntity();
        orderEntity.setOrderSn(UUID.randomUUID().toString());
        orderEntity.setModifyTime(new Date());
        rabbitTemplate.convertAndSend("order-event-exchange","order.create.order",orderEntity);
        return "ok";
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("order:order:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = orderService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("order:order:info")
    public R info(@PathVariable("id") Long id){
		OrderEntity order = orderService.getById(id);

        return R.ok().put("order", order);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("order:order:save")
    public R save(@RequestBody OrderEntity order){
		orderService.save(order);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("order:order:update")
    public R update(@RequestBody OrderEntity order){
		orderService.updateById(order);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("order:order:delete")
    public R delete(@RequestBody Long[] ids){
		orderService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
